package net.maku.association.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import net.maku.association.dao.InterestAssociationApplyDao;
import net.maku.association.dao.InterestAssociationMemberDao;
import net.maku.association.entity.InterestAssociationApplyEntity;
import net.maku.association.entity.InterestAssociationMemberEntity;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.association.convert.InterestAssociationConvert;
import net.maku.association.entity.InterestAssociationEntity;
import net.maku.association.query.InterestAssociationQuery;
import net.maku.association.vo.InterestAssociationVO;
import net.maku.association.dao.InterestAssociationDao;
import net.maku.association.service.InterestAssociationService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 兴趣协会
 *
 * @author 阿沐 babamu@126.com
 * @since 1.0.0 2025-04-26
 */
@Service
@AllArgsConstructor
public class InterestAssociationServiceImpl extends BaseServiceImpl<InterestAssociationDao, InterestAssociationEntity> implements InterestAssociationService {

    private final InterestAssociationMemberDao interestAssociationMemberDao;

    private final InterestAssociationApplyDao interestAssociationApplyDao;
    @Override
    public PageResult<InterestAssociationVO> page(InterestAssociationQuery query,Long currentUserId) {
        IPage<InterestAssociationEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));
        List<InterestAssociationVO> interestAssociations = InterestAssociationConvert.INSTANCE.convertList(page.getRecords());
        interestAssociations.forEach(a->{
            Long id = a.getId();
            QueryWrapper<InterestAssociationMemberEntity> memberQuery = new QueryWrapper<>();
            memberQuery.eq("association_id",id);
            Long count = interestAssociationMemberDao.selectCount(memberQuery);
            a.setMemberCount(count);

            QueryWrapper<InterestAssociationMemberEntity> joinQuery = new QueryWrapper<>();
            joinQuery.eq("association_id",id).eq("user_id",currentUserId);
            Long joinCount = interestAssociationMemberDao.selectCount(joinQuery);
            if(joinCount > 0){
                a.setIsJoin(1);
            }else{
                a.setIsJoin(0);
            }
        });
        return new PageResult<>(interestAssociations, page.getTotal());
    }

    private LambdaQueryWrapper<InterestAssociationEntity> getWrapper(InterestAssociationQuery query){
        LambdaQueryWrapper<InterestAssociationEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotBlank(query.getName()), InterestAssociationEntity::getName, query.getName())
                .eq(StringUtils.isNotBlank(query.getPresident()),InterestAssociationEntity::getPresident,query.getPresident());
        return wrapper;
    }

    @Override
    public void save(InterestAssociationVO vo) {
        InterestAssociationEntity entity = InterestAssociationConvert.INSTANCE.convert(vo);
        entity.setCreateTime(new Date());
        baseMapper.insert(entity);
    }

    @Override
    public void update(InterestAssociationVO vo) {
        InterestAssociationEntity entity = InterestAssociationConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

    @Override
    public InterestAssociationVO selectById(Long id,Long currentUserId) {
        InterestAssociationEntity interestAssociationEntity = baseMapper.selectById(id);
        InterestAssociationVO entity = InterestAssociationConvert.INSTANCE.convert(interestAssociationEntity);
        QueryWrapper<InterestAssociationMemberEntity> joinQuery = new QueryWrapper<>();
        joinQuery.eq("association_id",id).eq("user_id",currentUserId);
        Long joinCount = interestAssociationMemberDao.selectCount(joinQuery);
        if(joinCount > 0){
            entity.setIsJoin(1);
        }else{
            QueryWrapper<InterestAssociationApplyEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("association_id",id);
            queryWrapper.eq("user_id",currentUserId);
            queryWrapper.eq("audit_status",0);
            queryWrapper.orderByDesc("id");
            InterestAssociationApplyEntity interestAssociationApplyEntity = interestAssociationApplyDao.selectOne(queryWrapper);
            if(interestAssociationApplyEntity == null){
                entity.setIsJoin(0);
            }else{
                Integer auditStatus = interestAssociationApplyEntity.getAuditStatus();
                //待审核
                if(auditStatus == 0){
                    entity.setIsJoin(-1);
                }
            }
        }
        QueryWrapper<InterestAssociationMemberEntity> memberQuery = new QueryWrapper<>();
        memberQuery.eq("association_id",id);
        Long count = interestAssociationMemberDao.selectCount(memberQuery);
        entity.setMemberCount(count);
        return entity;
    }

}